﻿//======================================================================
//
//        Copyright (C) 2010 - 2012 Hydra    
//        All rights reserved
//
//        filename :HMenuItem
//        description :
//
//        created by Hydra at  3/9/2011 10:18:17 AM
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
using Deeper.Common;

namespace Deeper.Common.Web
{
	[Serializable]
	public class HMenuItem
	{
		/// <summary>
		/// 菜单名称
		/// </summary>
		public string ItemName { set; get; }
		/// <summary>
		/// 菜单地址
		/// </summary>
		public string Url { set; get; }
		/// <summary>
		/// 样式名称
		/// </summary>
		public string ClassName { set; get; }
		/// <summary>
		/// 内容备注
		/// </summary>
		public string Memo { set; get; }
		/// <summary>
		/// 是否有权限
		/// </summary>
		public bool IsHasPower { set; get; }
		/// <summary>
		/// 是否被选中
		/// </summary>
		public bool Selected { set; get; }
		/// <summary>
		/// 子项目列表
		/// </summary>
		public IList<HMenuItem> SubItems { private set; get; }


		public override string ToString()
		{
			string str = string.Format("名称:{0} 地址:{1} 样式:{2} 备注:{3}", ItemName, Url, ClassName, Memo);
			return str;
		}
		public HMenuItem()
		{
			SubItems = new List<HMenuItem>();
		}

		public HMenuItem(XmlNode node, string currentName = null, IHUser user = null, bool isAdd = false)
		{
			SubItems = new List<HMenuItem>();
			LoadFromXmlNode(node, currentName, user, isAdd);

		}

		/// <summary>
		/// 从Xml节点获取菜单信息
		/// </summary>
		/// <param name="node">XMl节点</param>
		/// <param name="currentName">当前菜单项名称</param>
		/// <param name="user">登录用户</param>
		/// <param name="isAdd">是否没权限也载入</param>
		private void LoadFromXmlNode(XmlNode node, string currentName = null, IHUser user = null, bool isAdd = false)
		{
			if (node.Attributes["Name"].IsNotNull())
				this.ItemName = node.Attributes["Name"].Value;

			if (node.Attributes["Css"].IsNotNull())
				this.ClassName = node.Attributes["Css"].Value;

			if (node.Attributes["Url"].IsNotNull())
				this.Url = node.Attributes["Url"].Value;

			if (node.SelectSingleNode("Memo").IsNotNull())
				this.Memo = node.SelectSingleNode("Memo").InnerText;

			this.Selected = (this.ItemName == currentName);

			if (user.IsNotNull()) this.IsHasPower = user.IsHasPower(this.ItemName);
			else this.IsHasPower = true;

		}
		/// <summary>
		/// 从Xml节点载入子项
		/// </summary>
		/// <param name="node">XMl节点</param>
		/// <param name="currentName">当前菜单项名称</param>
		/// <param name="user">登录用户</param>
		/// <param name="isAdd">是否没权限也载入</param>
		public void LoadSubItems(XmlNode node, string currentName = null, IHUser user = null, bool isAdd = false)
		{
			this.SubItems.Clear();
			node.SelectNodes("Menu").Cast<XmlNode>().ToList().ForEach(s =>
			{
				HMenuItem item = new HMenuItem(s, currentName, user, isAdd);
				this.SubItems.Add(item);
			});
		}


		/// <summary>
		/// 从XML文件获取菜单列表
		/// </summary>
		/// <param name="fileName">XML文件名,如果为空则从配置文件的MenuFile字段取值</param>
		/// <param name="parentName">如果不为空，则只取该菜单的子菜单，否则取全部</param>
		/// <param name="currentName">当前项名称</param>
		/// <param name="user">登录用户</param>
		/// <param name="isAdd">是否没权限也载入</param>
		/// <returns></returns>
		public static IList<HMenuItem> GetList(string fileName, string parentName, string currentName, IHUser user = null, bool isAdd = false)
		{
			List<HMenuItem> list = new List<HMenuItem>();
			try
			{
				if (fileName.IsNull()) fileName = "MenuFile".GetConfigString();
				if (fileName.IndexOf("~") == 0) fileName = System.Web.HttpContext.Current.Server.MapPath(fileName);

				XmlDocument doc = new XmlDocument();                 //获得配置文件的全路径 
				if (!FileHelper.FileExists(fileName)) throw new Exception("菜单文件不存在");
				doc.Load(fileName);
				XmlNodeList nodes = doc.GetElementsByTagName("MenuTop");
				nodes.Cast<XmlNode>().ToList().ForEach(Menus =>
				{
					HMenuItem h = new HMenuItem(Menus, currentName, user, isAdd);
					h.LoadSubItems(Menus, currentName, user, isAdd);
					if (parentName.IsNull())
						list.Add(h);
					else if (parentName == h.ItemName)
						list.AddRange(h.SubItems);
				});
			}
			catch { }
			return list;
		}
	}
}
